package DAO;

import DATA.User;
import DButils.ConnectTool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.*;
import java.util.List;

public class UserDAO {
    public static User FindbyName(String name) throws SQLException{
        ConnectTool utils=new ConnectTool();
        String sql="select * from users where username = ?";
        User ans= (User) utils.find(sql,new BeanHandler<User>(User.class),name);
        return ans;
    }
    public static User FindbyId(int id) throws SQLException {
        ConnectTool utils=new ConnectTool();
        String sql="select * from users where id = ?";
        User ans= (User) utils.find(sql,new BeanHandler<User>(User.class),id);
        return ans;
    }
    public static List<User> FindAll() throws SQLException {
        ConnectTool utils=new ConnectTool();
        String sql="select * from users?";
        List<User> ans= (List<User>) utils.find(sql,new BeanListHandler(User.class),null);
        return ans;
    }
    public static boolean modify(String ...Params) throws SQLException {
        ConnectTool utils=new ConnectTool();
        String sql="update users set Username= ?,Userpwd= ?,Email= ? where username= ?";
        boolean ans=utils.update(sql,Params);
        return ans;
    }
    public static boolean insertUser(User target) throws SQLException {
        ConnectTool utils=new ConnectTool();
        String sql="insert into users ( Username, Userpwd,Email ) values (?,?,?)";
        boolean ans=utils.insert(sql,new BeanHandler(User.class),target.getUsername(),target.getUserpwd(),target.getEmail());
        return ans;
    }
//    public static boolean deleteUser(User target) throws SQLException {
    public static boolean deleteUser(int uid) throws SQLException {
        ConnectTool utils=new ConnectTool();
        String sql="delete from users where id=?";
        boolean ans=utils.update(sql,uid);
        return ans;
    }

}
